package com.yksj.healthtalk.ui;

import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.server.ServerCenterMainUI;
import com.yksj.healthtalk.ui.server.ServerShopSeachActivity;
import com.yksj.healthtalk.utils.ToastUtil;
//服务中心列表
public class ServerCenterListActivity extends BaseFragmentActivity implements OnItemClickListener, OnClickListener {

	private ListView mListView;
	private myAdapter adapter;
	final DisplayMetrics metrics = new DisplayMetrics();
	int type;//类型 2,服务中心0  3,保健  4表示搜索出来的药品服务中心 从主页搜索过来
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.server_center_list_layout);
		initTitle();
		
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		
		mListView = (ListView)findViewById(R.id.list);
		adapter = new myAdapter();
		mListView.setAdapter(adapter);
		
		mListView.setOnItemClickListener(this);
		titleLeftBtn.setOnClickListener(this);
		this.type=getIntent().getIntExtra("type",1);
		if(4==type&&getIntent().hasExtra("json")){
			JSONObject jsonArray= JSONObject.parseObject(getIntent().getStringExtra("json"));
			adapter.onBountData(type, jsonArray.getJSONArray("DETAILS"));
			titleTextV.setText("药品");
		}else{
			seachAll("");
		}
	}
	
	private void seachAll(String value){
		//OPTION=ALL&KEYWORDS=高
		RequestParams params=new RequestParams(); 
		switch (type) {
		case 2://服务中心
			params.put("OPTION", "YIYUAN");
			titleTextV.setText("医院中心");
			break;
		case 3://体检保健
			params.put("OPTION", "BAOJIAN");
			titleTextV.setText("体检保健");
			break;
		case 5://医药商家
			params.put("OPTION", "ShangJia");
			titleTextV.setText("医药展会");
			break;
		case 6://专业学会
			params.put("OPTION", "YiXueHui");
			titleTextV.setText("专业学会");
			break;
		case 7://行业协会
			params.put("OPTION", "HangYeXieHui");
			titleTextV.setText("行业协会");
			break;
		case 4://药品购买
			params.put("OPTION", "ALL");
			params.put("KEYWORDS", value);
			titleTextV.setText("药品");
			break;
		}
		HttpRestClient.doHttpHomePageQueryServlet(params,new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
				if(response.containsKey("error_message")){
					ToastUtil.showShort(response.getString("error_message"));
				}else{
					JSONArray jsonArray = response.getJSONArray("RESULT");
					adapter.onBountData(type, jsonArray);
				}
			}
		});
	}

	private class myAdapter extends BaseAdapter{
		final JSONArray jsonArray;
		LayoutInflater inflater;
		private int type;
		private ImageLoader mInstance;
		
		public myAdapter() {
			super();
			this.inflater = LayoutInflater.from(ServerCenterListActivity.this);
			jsonArray=new JSONArray();
			mInstance = ImageLoader.getInstance();
		}

		@Override
		public int getCount() {
			return jsonArray.size();
		}
		
		public void onBountData(int type,JSONArray jsonArray){
			this.type=type;
			this.jsonArray.clear();
			this.jsonArray.addAll(jsonArray);
			notifyDataSetChanged();
		}
		
		@Override
		public JSONObject getItem(int arg0) {
			return (JSONObject) jsonArray.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ViewHolder holder;
			if(arg1==null){
				holder=new ViewHolder();
				arg1=onCreateView(type,holder);
				arg1.setTag(holder);
			}
			doBoundData(type,(ViewHolder)arg1.getTag(),getItem(arg0));
			return arg1;
		}
		
		public void doBoundData(int type, ViewHolder holder,JSONObject jsonObject) {
			switch (type) {
			case 1:
				holder.icon.setVisibility(View.VISIBLE);
				holder.name.setText(jsonObject.getString("NAME"));
				holder.number.setText(jsonObject.getString("COUNT"));
				String path=jsonObject.getString("ICON");
				if(path.contains("zhuanjia")){
					holder.icon.setBackgroundResource(R.drawable.seach_zhuanjia);
				}else if(path.contains("yaopin")){
					holder.icon.setBackgroundResource(R.drawable.seach_yaopin);
				}else if(path.contains("shalong")){
					holder.icon.setBackgroundResource(R.drawable.seach_salon);
				}else if(path.contains("bingyou")){
					holder.icon.setBackgroundResource(R.drawable.seach_bingyou);
				}else if(path.contains("fabu")){
					holder.icon.setBackgroundResource(R.drawable.seach_fabu);
				}else if(path.contains("zhengzhuang")){
					holder.icon.setBackgroundResource(R.drawable.seach_zhengzhuang);
				}else if(path.contains("jibing")){
					holder.icon.setBackgroundResource(R.drawable.seach_jibing);
				}else if(path.contains("baojian")){
					holder.icon.setBackgroundResource(R.drawable.seach_baojian);
				}else if(path.contains("yiyuan")){
					holder.icon.setBackgroundResource(R.drawable.seach_yiyuan);
				}else{
					holder.icon.setVisibility(View.GONE);
				}
				break;
			case 2:
			case 3:
			case 5:
			case 6:
			case 7:
				mInstance.displayImage(jsonObject.getString("ANDROID_LIST_ICON"), holder.icon);
				holder.name.setText(jsonObject.getString("MERCHANT_NAME"));
				holder.icon.setVisibility(View.VISIBLE);
				holder.number.setText("");
				break;
			case 4:
				holder.icon.setVisibility(View.INVISIBLE);
				holder.name.setText(jsonObject.getString("NAME"));
				holder.number.setText(jsonObject.getString("COUNT"));
				break;
			}
			
		} 
		
		
		public  class ViewHolder{
			public ImageView icon;
			public TextView name;
			public TextView number;
		}
		
		private View onCreateView(int type,ViewHolder holder){
			View view = null;
			switch(type){
			case 1://
			case 2://
			case 3://
			case 4://
			case 5://
			case 6://
			case 7://
				view = inflater.inflate(R.layout.main_seach_all_item,null);
				holder.icon=(ImageView) view.findViewById(R.id.icon_view);
				holder.name=(TextView) view.findViewById(R.id.name_type);
				holder.number=(TextView) view.findViewById(R.id.number);
				break;
			}
			return view;
		}
		
	}



	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			final String MERCHANT_ID=adapter.getItem(arg2).getString("MERCHANT_ID");
			if(this.type==4){
				Intent intent = new Intent(this,ServerShopSeachActivity.class);
				intent.putExtra("main", getIntent().getStringExtra("result"));
				intent.putExtra("type", -3);
				intent.putExtra("MERCHANT_ID", MERCHANT_ID);
				startActivity(intent);
			}else{
				HttpRestClient.doHttpServerCneterBg(String.valueOf(metrics.widthPixels), String.valueOf(metrics.heightPixels), SmartFoxClient.getLoginUserId(),MERCHANT_ID,
						new JsonsfHttpResponseHandler(this){
					@Override
					public void onSuccess(int statusCode,JSONObject response) {
						super.onSuccess(statusCode, response);
						String str = response.getString("VALID_FLAG");
						if ("2".equals(str)) {// 不可以进入
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "即将开通,敬请期待!");
						} else {
							Intent intent=new Intent(ServerCenterListActivity.this,ServerCenterMainUI.class);
							Bundle bundle=new Bundle();
							bundle.putString("content", response.toString());
							bundle.putString("MERCHANT_ID",MERCHANT_ID);
							intent.putExtras(bundle);
							startActivity(intent);
						}
					}
				});
				
			}
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		}
		
	}
}
